import util
import uix
import uiutil
import form
import listentry
import uiconst
import uicls

class VirtualGroupWindow(uicls.Window):
    __guid__ = 'form.VirtualGroupWindow'

    def ApplyAttributes(self, attributes):
        uicls.Window.ApplyAttributes(self, attributes)
        node = attributes.node
        caption = (attributes.caption or 'List window')
        self.SetScope('station_inflight')
        self.SetMinSize((200, 200))
        self.SetTopparentHeight(0)
        self.sr.data = node.copy()
        main = uiutil.GetChild(self, 'main')
        uix.Flush(main)
        icon = getattr(self.sr.data, 'showicon', '')
        if (icon == 'hide'):
            self.SetCaption(caption)
            self.SetWndIcon('ui_9_64_14')
        elif (icon and (icon[0] == '_')):
            self.SetWndIcon(icon[1:], 1, size=32)
            self.SetCaption(caption)
        else:
            self.SetCaption(caption)
            self.SetWndIcon('ui_22_32_29', 1, size=32)
            if icon:
                mainicon = uiutil.GetChild(self, 'mainicon')
                uix.MapSprite(icon, mainicon, size=32)
                mainicon.state = uix.UI_DISABLED
            else:
                self.SetWndIcon('ui_22_32_29', 1, size=32)
        self.sr.scroll = uicls.Scroll(name='scroll', parent=main, padding=(const.defaultPadding,
         const.defaultPadding,
         const.defaultPadding,
         const.defaultPadding), align=uiconst.TOALL)
        self.sr.scroll.sr.iconMargin = getattr(self.sr.data, 'iconMargin', 0)
        ignoreTabTrimming = (util.GetAttrs(node, 'scroll', 'sr', 'ignoreTabTrimming') or 0)
        self.sr.scroll.sr.ignoreTabTrimming = ignoreTabTrimming
        minColumnWidth = (util.GetAttrs(node, 'scroll', 'sr', 'minColumnWidth') or {})
        self.sr.scroll.sr.minColumnWidth = minColumnWidth
        self.sr.scroll.sr.content.OnDropData = self.OnDropData



    def OnClose_(self, *args):
        self.sr.data = None



    def LoadContent(self, newNode = None, newCaption = None):
        if ((not self) or self.destroyed):
            return 
        if newNode:
            self.sr.data = newNode.Copy()
        if newCaption:
            self.SetCaption(newCaption)
        if self.sr.data.GetSubContent:
            content = self.sr.data.GetSubContent(self.sr.data, 1)
        else:
            raise RuntimeError('LoadContent: WTF')
        if self.sr.data.scroll.sr.id:
            self.sr.scroll.sr.id = ('%s_%s' % (self.sr.data.scroll.sr.id, self.sr.data.id))
        self.sr.scroll.sr.fixedColumns = self.sr.data.scroll.sr.fixedColumns.copy()
        self.sr.scroll.Load(contentList=content, headers=self.sr.data.scroll.GetColumns(), fixedEntryHeight=self.sr.data.scroll.sr.fixedEntryHeight, scrolltotop=0)



    def OnDropData(self, dragObj, nodes):
        if getattr(self.sr.data, 'DropData', None):
            self.sr.data.DropData(self.sr.data.id, nodes)
            return 
        ids = []
        myListGroupID = self.sr.data.id
        for node in nodes:
            if (node.__guid__ not in self.sr.data.get('allowGuids', [])):
                uix.LogWarn('dropnode.__guid__ has to be listed in group.node.allowGuids', node.__guid__, getattr(self.sr.data, 'allowGuids', []))
                continue
            if not node.Get('itemID', None):
                uix.LogWarn('dropitem data has to have itemID')
                continue
            currentListGroupID = node.Get('listGroupID', None)
            ids.append((node.itemID,
             currentListGroupID,
             myListGroupID))

        for (itemID, currentListGroupID, myListGroupID,) in ids:
            if (currentListGroupID and itemID):
                uicore.registry.RemoveFromListGroup(currentListGroupID, itemID)
            uicore.registry.AddToListGroup(myListGroupID, itemID)

        uicore.registry.ReloadGroupWindow(myListGroupID)
        if getattr(self.sr.data, 'RefreshScroll', None):
            self.sr.data.RefreshScroll()




class ListGroup(uicls.SE_ListGroupCore):
    __guid__ = 'listentry.Group'
    __resPath__ = 'res:/UI/Component/listgroup.blue'
    __notifyevents__ = ['OnSetComponentColor']

    def Startup(self, *etc):
        self.sr.expander = uicls.Icon(uiutil.GetChild(self, 'expander'))
        self.sr.expander.OnClick = self.Toggle
        self.sr.expander.anchors = uiconst.UI_ANCHLEFT
        self.sr.expander.top = 1
        self.sr.expander.SetAlpha(0.8)
        self.sr.icon = uicls.Icon(uiutil.GetChild(self, 'icon'))
        self.sr.ontopicon = uiutil.GetChild(self, 'ontopicon')
        self.sr.labelClipper = uicls.Container(parent=self, align=uiconst.TOALL, pos=(0,
         0,
         const.defaultPadding,
         0), clipChildren=1)
        self.sr.label = uicls.Label(text='', parent=self.sr.labelClipper, left=5, state=uiconst.UI_DISABLED, singleline=1, idx=0, align=uiconst.CENTERLEFT)
        self.sr.topLine = uiutil.GetChild(self, 'topline')
        self.sr.selection = uiutil.GetChild(self, 'selection')
        self.sr.selection.height += 1
        self.sr.selection.state = uiconst.UI_HIDDEN
        self.sr.fill = uicls.Fill(parent=self, top=-1, height=-1)
        defComponent = sm.GetService('window').GetDefaultComponentColor()
        self.sr.fill.color.SetRGB(*defComponent)
        self.sr.mainLinePar = uicls.Container(parent=self, align=uiconst.TOALL, idx=0, pos=(0, 0, 0, -15), state=uix.UI_DISABLED)
        self.sr.bottomLineContainer = uicls.Container(parent=self.sr.mainLinePar, align=uiconst.TOBOTTOM, height=16)
        self.sr.bottomLineLeft = uicls.Icon(icon='ui_73_16_38', parent=self.sr.bottomLineContainer, size=16, scaling=(16 / 256.0), align=uiconst.TOLEFT, state=uiconst.UI_NORMAL)
        self.sr.bottomLineArrow = uicls.Icon(icon='ui_73_16_39', parent=self.sr.bottomLineContainer, size=16, align=uiconst.TOLEFT, state=uiconst.UI_NORMAL)
        bottomLineStretch = self.sr.bottomLineLeft.CopyTo()
        bottomLineStretch.width = 0
        bottomLineStretch.height = 0
        bottomLineStretch.rectLeft += 2
        bottomLineStretch.rectWidth -= 4
        bottomLineStretch.align = uix.UI_ALCLIENT
        self.sr.bottomLineContainer.children.append(bottomLineStretch)
        sm.RegisterNotify(self)



    def OnSetComponentColor(self, color):
        if not self.destroyed:
            self.sr.fill.color.SetRGB(color.r, color.g, color.b, color.a)



    def GetHeight(self, *args):
        (node, width,) = args
        node.height = (uix.GetTextHeight(node.label, autoWidth=1, singleLine=1) + 4)
        return node.height



    def Load(self, node):
        self.sr.node = node
        self.sr.id = node.id
        self.sr.subitems = (node.get('subitems', []) or node.get('groupItems', []))
        self.UpdateLabel()
        self.hint = node.Get('hint', '')
        sublevel = node.Get('sublevel', 0)
        self.sr.expander.left = ((16 * sublevel) + 3)
        self.sr.label.left = ((20 + (16 * sublevel)) + 20)
        self.sr.icon.left = ((16 * sublevel) + 20)
        self.sr.node.selectable = node.Get('selectGroup', 0)
        self.sr.selection.state = [uix.UI_HIDDEN, uix.UI_DISABLED][node.Get('selected', 0)]
        self.sr.expander.state = [uix.UI_NORMAL, uix.UI_HIDDEN][node.Get('hideExpander', 0)]
        self.sr.fill.state = [uix.UI_DISABLED, uix.UI_HIDDEN][node.Get('hideFill', False)]
        self.sr.topLine.state = [uix.UI_DISABLED, uix.UI_HIDDEN][node.Get('hideTopLine', False)]
        self.UpdateBottomLine()
        if (self.sr.expander.state == uix.UI_HIDDEN):
            self.sr.labelClipper.width = 0
        for (k, v,) in node.Get('labelstyle', {}).iteritems():
            setattr(self.sr.label, k, v)

        icon = node.Get('showicon', '')
        iconID = node.Get('iconID', None)
        if iconID:
            self.sr.icon.ChangeIcon(iconID=iconID, size=16, ignoreSize=True)
            self.sr.icon.state = uix.UI_DISABLED
            self.sr.ontopicon.state = uix.UI_HIDDEN
        elif (icon == 'hide'):
            self.sr.icon.state = uix.UI_HIDDEN
            self.sr.label.left -= 20
        elif (icon and (icon[0] == '_')):
            uix.MapSprite(icon[1:], self.sr.icon, scaling=0.125)
            self.sr.icon.state = uix.UI_DISABLED
            self.sr.ontopicon.state = uix.UI_HIDDEN
        else:
            uix.MapSprite('22_29', self.sr.icon, scaling=0.125)
            self.sr.icon.state = uix.UI_DISABLED
            if icon:
                uix.MapSprite(icon, self.sr.icon, scaling=0.125)
                self.sr.ontopicon.state = uix.UI_DISABLED
            else:
                self.sr.ontopicon.state = uix.UI_HIDDEN
            self.sr.icon.width = 16
        if (self.sr.expander.state == uiconst.UI_HIDDEN):
            self.sr.label.left -= 16
            self.sr.icon.left -= 18
        if ((node.panel is not self) or (self is None)):
            return 
        if (self.sr.icon.state != uix.UI_HIDDEN):
            if (self.sr.icon.width != self.sr.icon.rectWidth):
                self.sr.icon.tripass = eve.rot.GetInstance('res:/UI/Pass/linear.blue')
        self.ShowOpenState(uicore.registry.GetListGroupOpenState(self.sr.id, default=node.Get('openByDefault', False)))
        self.RefreshGroupWindow(0)



    def OnDblClick(self, *args):
        if self.sr.node.Get('OnDblClick', None):
            self.sr.node.OnDblClick(self)
            return 
        if self.sr.node.Get('BlockOpenWindow', 0):
            return 
        self.RefreshGroupWindow(create=1)



    def RefreshGroupWindow(self, create):
        if self.sr.node:
            wnd = sm.GetService('window').GetWindow(unicode(self.sr.node.id), create=create, decoClass=form.VirtualGroupWindow, node=self.sr.node, caption=self.sr.node.label.replace('<t>', '-'))
            if wnd:
                wnd.LoadContent(self.sr.node, newCaption=self.sr.node.label)
                if create:
                    wnd.Maximize()
                if ((not self) or self.destroyed):
                    return 
                node = self.sr.node
                if node.open:
                    self.Toggle()



    def GetNoItemEntry(self):
        return listentry.Get('Generic', {'label': mls.UI_GENERIC_NOITEM,
         'sublevel': (self.sr.node.Get('sublevel', 0) + 1)})



    def GetMenu(self):
        m = []
        if not self.sr.node.Get('BlockOpenWindow', 0):
            wnd = sm.GetService('window').GetWindow(unicode(self.sr.node.id))
            if wnd:
                m = [(mls.UI_CMD_SHOWWINDOW,
                  self.RefreshGroupWindow,
                  (1,))]
            else:
                m = [(mls.UI_CMD_OPENGROUPWINDOW,
                  self.RefreshGroupWindow,
                  (1,))]
        node = self.sr.node
        expandable = node.Get('expandable', 1)
        if expandable:
            if not node.open:
                m += [(mls.UI_CMD_EXPAND,
                  self.Toggle,
                  ())]
            else:
                m += [(mls.UI_CMD_COLLAPSE,
                  self.Toggle,
                  ())]
        if (node.Get('state', None) != 'locked'):
            m += [(mls.UI_CMD_CHANGELABEL, self.ChangeLabel)]
            m += [(mls.UI_CMD_DELETEFOLDER, self.DeleteFolder)]
        if node.Get('MenuFunction', None):
            cm = node.MenuFunction(node)
            m += cm
        return m



    def GetNewGroupName(self):
        return uix.NamePopup(mls.UI_GENERIC_TYPEINNEWNAME, mls.UI_GENERIC_TYPEINNEWNAMEFOLDER)



    def CloseWindow(self, windowID):
        wnd = sm.GetService('window').GetWindow(windowID)
        if wnd:
            wnd.SelfDestruct()



    def OnDragEnter(self, dragObj, drag, *args):
        if self.sr.node.Get('DragEnterCallback', None):
            self.sr.node.DragEnterCallback(self, drag)
        elif (drag and (getattr(drag[0], '__guid__', None) in (self.sr.node.Get('allowGuids', []) + ['xtriui.DragIcon']))):
            self.sr.selection.state = uiconst.UI_DISABLED



    def OnDragExit(self, dragObj, drag, *args):
        if (self.sr.selection and (not self.sr.node.selected)):
            self.sr.selection.state = uiconst.UI_HIDDEN



    def ShowOpenState(self, open_):
        self.sr.expander.rectTop = [75, 64][open_]
        if self.sr.expander:
            if open_:
                self.sr.expander.ChangeIcon(icon='ui_38_16_229')
            else:
                self.sr.expander.ChangeIcon(icon='ui_38_16_228')



    def UpdateBottomLine(self):
        isOpen = uicore.registry.GetListGroupOpenState(self.sr.id, default=self.sr.node.Get('openByDefault', True))
        self.sr.bottomLineLeft.state = [uix.UI_HIDDEN, uix.UI_NORMAL][(self.sr.node.Get('hasArrow', False) and isOpen)]
        self.sr.bottomLineArrow.state = [uix.UI_HIDDEN, uix.UI_NORMAL][(self.sr.node.Get('hasArrow', False) and isOpen)]
        self.sr.bottomLineLeft.width = (self.sr.node.Get('sublevel', 0) * 16)




